home *** CD-ROM | disk | FTP | other *** search
/ IRIS Performer 2.2 Friends Demo / SGI IRIS Performer 2.2 Friends Demo.iso / friends / devices / BGSystems / bg / read_write.c < prev   
C/C++ Source or Header  |  1997-10-31  |  4KB  |  224 lines

  1. /*
  2.  * Copyright 1992-4   BG Systems, Inc.
  3.  * read_write.c
  4.  *
  5.  * routines that are to be called to read, & write to the LV824
  6.  *
  7.  * Author         Date       Comments
  8.  * John Green     21-Oct-94  Major revision for v 3.0 EPROM
  9.  * John Green     15-Jan-95  Updated for LV824.
  10.  * John Green     01-Feb-95  Final touches for release 3.0
  11.  */
  12.  
  13. static char SccsId[] = "@(#)read_write.c    1.4 24 Feb 1995";
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18.  
  19. #include "lv3.h"
  20.  
  21. extern RS_ERR rs_err;
  22.  
  23.  
  24. int w_lv(int, char *);
  25.  
  26. extern int pack_data(bglv *, char *);
  27. extern int convert_serial(bglv *, char *);
  28.  
  29. extern int sginap(long );
  30.  
  31. int send_outputs(bglv *bgp)
  32. {
  33.    int st;
  34.    char buf[32];
  35.    long count;
  36.  
  37. /*
  38.  *  Pack the numbers into a character string
  39.  */
  40.    st = pack_data(bgp, buf);
  41.   
  42.    count = strlen(buf);
  43. /*
  44.  *  Simply write the characters to the output serial port (ofd)
  45.  */
  46.    st = write(bgp->sp_fd, buf, strlen(buf));
  47.    if (st != count)
  48.    {
  49.       printf("error: write()\nOSErr: %d\tcount:  %ld\n",st, count);
  50.    }
  51.    return(st);
  52. }
  53.  
  54. int check_inputs(bglv *bgp)
  55. {
  56.    int st;
  57.    int i;
  58.    char str[36];
  59.  
  60.    rs_err.cycles++;
  61.    if( rs_err.cycles % 1000 == 0 )
  62.    {
  63.       rs_err.cycles = 0;
  64.       rs_err.thou++;
  65.    }
  66.  
  67. /* 
  68.  *  Read the serial port
  69.  */
  70.    st = read(bgp->sp_fd,str,bgp->str_len);
  71.  
  72. /*
  73.  *  Read error
  74.  */
  75.    if (st < 0)
  76.    {
  77.       rs_err.rd++;
  78.       printf("Major read error\n");
  79.       return(-1);
  80.    }
  81.  
  82.    i = 0;
  83. /*
  84.  *  We expect to get str_len characters -- if not, we will re-read 
  85.  *  the port 100 times.
  86.  *  If you repeatedly get messages when you are running this code
  87.  *  to the effect that the characters were read after 25 re-tries,
  88.  *  then you are trying to sample too fast.  If you repeatedly get
  89.  *  100 re-tries, then you are probably have no communication at all.
  90.  *  Check the chapter on "trouble shooting".
  91.  */
  92.    while ( st != bgp->str_len && i < 100)
  93.    {
  94. /*
  95.  *  Check for LV824 requesting a handshake -- communication may have been 
  96.  *  interupted, so we need to send an 'h' back.
  97.  */
  98.       if ( str[0] == 'h' )
  99.       {
  100.          printf("Handshake requested (%d)\n", i);
  101.          w_lv(bgp->sp_fd, "h");
  102.          sginap(10);
  103.          return(-1);
  104.       }
  105. /**
  106.       sginap(1);
  107. **/
  108.       st = read(bgp->sp_fd,str,bgp->str_len);
  109.       i++;
  110.    }
  111.    if ( i > 0 )
  112.       printf(" %d read attempts.\n", i);
  113.  
  114.    if (str[0] != 'p' || str[bgp->str_len - 1] != '\n')
  115.    {
  116.       printf("Unexpected string :  %s\n", str);
  117.       rs_err.rd++;
  118.       return(-1);
  119.    }
  120.    if (str[0] == '\n' )
  121.       return (1);
  122.  
  123. /*
  124.  *  Aha.  We got some real data !  So convert it from characters
  125.  *  to meaningful numbers, and put them in the bgp data structure.
  126.  */
  127.    st = convert_serial(bgp, str);
  128.  
  129.    return st;
  130. }
  131.  
  132. int r_lv(bglv *bgp)
  133. {
  134.    int st;
  135.    int i = 0;
  136.    char str[36];
  137.  
  138.    rs_err.cycles++;
  139.    if( rs_err.cycles % 1000 == 0 )
  140.    {
  141.       rs_err.cycles = 0;
  142.       rs_err.thou++;
  143.    }
  144.  
  145.    st = read(bgp->sp_fd,str,bgp->str_len);
  146.    if (st < 0)
  147.    {
  148.       rs_err.rd++;
  149.       printf("r_lv():  read error\n");
  150.       return(-1);
  151.    }
  152.    while ( st != bgp->str_len && i < 100)
  153.    {
  154. /*
  155.       sginap(1);
  156. */
  157.       st = read(bgp->sp_fd,str,bgp->str_len);
  158.       i++;
  159.    }
  160.    if ( i > 0 )
  161.       printf("%d read attempts.  \n", i);
  162.  
  163.    if (str[0] != 'B' || str[bgp->str_len - 1] != '\n')
  164.    {
  165.       printf("%d:  %s\n", st, str);
  166.       rs_err.rd++;
  167.       return(-1);
  168.    }
  169.  
  170.    st = convert_serial(bgp, str);
  171.  
  172.    return st;
  173. }
  174.  
  175. int w_lv(int sp_fd, char *mode)
  176. {
  177.    int st;
  178.  
  179.    st = write(sp_fd, mode, strlen(mode));
  180.    if (st < 0)
  181.       rs_err.wrt++;
  182.    return(st);
  183. }
  184.  
  185. int get_ack(int sp_fd)
  186. {
  187.    int st;
  188.    int i = 0;
  189.    int chars = 2;
  190.    char str[36];
  191.  
  192.    st = read(sp_fd,str,chars);
  193.    if (st < 0)
  194.    {
  195.       printf("get_ack():  read error\n");
  196.       return(-1);
  197.    }
  198.    while ( st != 2 && i < 10000)
  199.    {
  200.       sginap(1);
  201.       st = read(sp_fd,str,chars);
  202.       i++;
  203.    }
  204.    if ( i > 1000 )
  205.       printf("Timeout %d chars in buffer \n", chars);
  206.  
  207.    if (str[0] == 'a' )
  208.    { 
  209.       printf("Setup OK\n"); 
  210.       return (0); 
  211.    }
  212.    else if (str[0] == 'f' )
  213.    { 
  214.       printf("Setup failed\n"); 
  215.       return (-1); 
  216.    }
  217.    else 
  218.    { 
  219.       printf("Unexpected respons: %s\n", str); 
  220.       return (-2); 
  221.    }
  222.    return(st);
  223. }
  224.